﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TS.Commons;
using TS.Entities;
using System.Data;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Globalization;

namespace TS.DataAccess
{
    /// <summary>
    /// Author: VuongPT
    /// </summary>
    public class NumberBikeDataAccess
    {
        /// <summary>
        /// Parse to Entity
        /// </summary>
        /// <param name="dataRecord"></param>
        /// <returns></returns>
        public static NumberBike ParseToEntity(IDataRecord dataRecord)
        {
            return new NumberBike
                       {
                           NumberBikeId = dataRecord["NumberBikeId"].ToStringOrDefault().ToNumeric(),
                           NumberPlate = dataRecord["NumberPlate"].ToStringOrDefault(),
                           Name = dataRecord["Name"].ToStringOrDefault(),
                           Tel = dataRecord["Tel"].ToStringOrDefault(),
                           Description = dataRecord["Description"].ToStringOrDefault()
                       };
        }

        /// <summary>
        /// Get All NumberBike
        /// </summary>
        /// <returns></returns>
        public Collection<NumberBike> GetAllNumberBike()
        {
            var collection = new Collection<SqlParameter>
                                 {
                                 };
            int totalRecord = 0;
            using (IDataReader reader = SqlHelper.ExecuteReader(UINumberBike.SelectAllNumberBikeQuery, collection))
            {
                var result = new Collection<NumberBike>();
                while (reader.Read())
                {
                    NumberBike obj = ParseToEntity(reader);
                    result.Add(obj);
                }
                if (reader.NextResult())
                {
                    if (reader.Read())
                    {
                        totalRecord = reader["TotalRowCount"].ToNumeric();
                    }
                }
                return result;
            }

        }

        /// <summary>
        /// Insert NumberBike
        /// </summary>
        /// <param name="numberBike"></param>
        /// <returns></returns>
        public int InsertNumberBike(NumberBike numberBike)
        {
            int result = 0;
            if (numberBike != null)
            {
                var parameters = new Collection<SqlParameter>
                                     {
                                         new SqlParameter("@NumberPlate", numberBike.NumberPlate),
                                         new SqlParameter("@Name", numberBike.Name),
                                         new SqlParameter("@Tel", numberBike.Tel),
                                         new SqlParameter("@Description", numberBike.Description)
                                     };

                using (IDataReader reader = SqlHelper.ExecuteReader(UINumberBike.InsertNumberBikeQuery, parameters))
                {
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            result = Convert.ToInt32(reader["NumberBikeId"], CultureInfo.CurrentCulture);
                        }
                        return result;
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// Read NumberBike Pagging
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strFilter"></param>
        /// <param name="strOrderBy"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public Collection<NumberBike> ReadNumberBikePaged(int pageSize, int pageIndex, string strFilter,
                                                          string strOrderBy, out int totalRecord)
        {
            var parameters = new Collection<SqlParameter>
                                 {
                                     new SqlParameter("@PageSize", pageSize),
                                     new SqlParameter("@PageIndex", pageIndex),
                                     new SqlParameter("@StringFilter", strFilter),
                                     new SqlParameter("@OrderBy", strOrderBy)
                                 };

            totalRecord = 0;

            //get result
            using (IDataReader reader = SqlHelper.ExecuteReader(UINumberBike.NumberBikeSelectPaged, parameters))
            {
                var result = new Collection<NumberBike>();
                while (reader.Read())
                {
                    NumberBike numberBike = ParseToEntity(reader);
                    numberBike.ShortDescription = Utility.WriteString(numberBike.Description, 20);
                    result.Add(numberBike);
                }
                if (reader.NextResult())
                {
                    if (reader.Read())
                    {
                        totalRecord = reader["TotalRowCount"].ToNumeric();
                    }
                }
                return result;
            }
        }

        /// <summary>
        /// get NumberBike by Id
        /// </summary>
        /// <param name="numberBikeId"></param>
        /// <returns></returns>
        public NumberBike GetByNumberBikeId(int numberBikeId)
        {
            var collection = new Collection<SqlParameter>
                                 {
                                     new SqlParameter
                                         (
                                         "@NumberBikeId",
                                         numberBikeId
                                         )
                                 };

            using (IDataReader iDataReader = SqlHelper.ExecuteReader(UINumberBike.NumberBikeSelectById, collection))
            {
                if (iDataReader.Read())
                {
                    return ParseToEntity(iDataReader);
                }
            }
            return null;
        }

        /// <summary>
        /// update NumberBike
        /// </summary>
        /// <param name="numberBike"></param>
        /// <returns></returns>
        public int UpdateNumberBike(NumberBike numberBike)
        {
            int result = 0;
            if (numberBike != null)
            {
                var parameters = new Collection<SqlParameter>
                                     {
                                         new SqlParameter("@NumberBikeId", numberBike.NumberBikeId),
                                         new SqlParameter("@NumberPlate", numberBike.NumberPlate),
                                         new SqlParameter("@Name", numberBike.Name),
                                         new SqlParameter("@Tel", numberBike.Tel),
                                         new SqlParameter("@Description", numberBike.Description)
                                     };

                using (IDataReader reader = SqlHelper.ExecuteReader(UINumberBike.UpdateNumberBike, parameters))
                {
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            result = Convert.ToInt32(reader["NumberBikeId"], CultureInfo.CurrentCulture);
                        }
                        return result;
                    }
                }

            }
            return result;
        }

        /// <summary>
        /// DeleteNumberBike
        /// </summary>
        /// <param name="numberBike"></param>
        /// <returns></returns>
        public int DeleteNumberBike(NumberBike numberBike)
        {
            int result = 0;
            if (numberBike != null)
            {
                var parameters = new Collection<SqlParameter>
                                     {
                                         new SqlParameter("@NumberBikeId", numberBike.NumberBikeId)
                                     };

                using (IDataReader reader = SqlHelper.ExecuteReader(UINumberBike.DeleteNumberBike, parameters))
                {
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            result = Convert.ToInt32(reader["NumberBikeId"], CultureInfo.CurrentCulture);
                        }
                        return result;
                    }
                }
            }

            return result;
        }
    }

}
